package com.android.dwt.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author xlees
 *
 */
public class DBOpenHelper extends SQLiteOpenHelper {
	
	private final static String DATABASE_NAME = "dwt.db";
    private final static int DATABASE_VERSION = 1;
    
    private final static String TB_LIST = "list";
	private final static String TB_TASK = "task";
    private static final String TB_LIST_CREATE = "create table " + TB_LIST
    		+ " (_id integer primary key autoincrement, "
		    + "list_name text not null, "
		    + "list_note text);";
    private static final String TB_TASK_CREATE = "create table " + TB_TASK
			+ " (_id integer primary key autoincrement, "
			+ "task_name text not null, "
			+ "task_note text, "
			+ "task_list text not null, "
			+ "task_context text, "
			+ "task_project text, "
			+ "task_pos text, "
			+ "task_status integer not null, "
			+ "task_st text, "
			+ "task_et text, "
			+ "task_repeat text, "
			+ "task_remind text, "
			+ "task_prior text not null, "
			+ "task_tag text, "
			+ "task_completed integer not null, "
			+ "task_created text not null"
			+ ");";
    
	public DBOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TB_LIST_CREATE);
		db.execSQL(TB_TASK_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DATABASE_NAME, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TB_LIST);
		db.execSQL("DROP TABLE IF EXISTS " + TB_TASK);
		onCreate(db);
	}
}
